#include<array>
#include<vector>
#include<unordered_map>
using namespace std;
class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        //dp[j][i]=dp[k][j]+1
        array<array<int,1000>,1000>dp;
        for(auto&e:dp)e.fill(0);      
        unordered_map<long long,vector<int>>u_map;
        u_map[nums[0]].push_back(0);
        int ans=0;
        for(int j=1;j<nums.size();j++){
            for(int i=j+1;i<nums.size();i++){
                long long k=2*(long long)nums[j]-nums[i];
                if(u_map.count(k)){
                    for(auto e:u_map[k]){
                        dp[j][i]+=dp[e][j]+1;
                    }
                }
                ans+=dp[j][i];
            }
            u_map[nums[j]].push_back(j);
        }
        return ans;
    }
};